﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;

public partial class controls_admin_Modules : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                loadCategories();
                base.OpenConnection();
                int c_id;
                int l_id;
                if (Request["l_id"] != null && Request["c_id"] != null)
                {
                    c_id = Convert.ToInt32(Request["c_id"]);
                    ddlCategories.SelectedValue = c_id.ToString();
                    l_id = Convert.ToInt32(Request["l_id"]);
                    string location = String.Empty;
                    switch (l_id)
                    {
                        case 1:
                            location = "Banner Modules";
                            break;
                        case 2:
                            location = "Modules giữa trang";
                            break;
                        case 3:
                            location = "Modules bên phải";
                            break;
                        default:
                            location = "Modules";
                            break;
                    }
                    if (c_id != -1 && c_id != -2)
                    {
                        DataTable categoryDT = BO_Categories.Search(c_id);
                        if (categoryDT != null && categoryDT.Rows.Count > 0)
                            title.InnerText = "Danh sách " + location + " thuộc danh mục " + categoryDT.Rows[0]["FULL_CATEGORY_NAME"].ToString();
                    }
                    else if (c_id == -1)
                        title.InnerText = "Danh sách các " + location + ".";
                    else
                        title.InnerText = "Modules hiển thị ngoài trang chủ " + location + ".";
                    loadModule(c_id, l_id);
                }
                else
                    divMessage.InnerHtml = O2S_Message.Warning("Thông Báo", "Bản ghi này không tồn tại hoặc đã bị xóa .");
            }
            catch { }
            finally { base.CloseConnection(); }
        }
    }
    protected void loadModule(int c_id, int l_id)
    {
        try
        {
            base.OpenConnection();
            DataTable dtModule;
            if (c_id != -1 && c_id != -2)
                dtModule = BO_Module.SelectLocationAdmin(l_id, c_id);
            else if (c_id == -1)
                dtModule = BO_Module.SelectLocationHomeAdmin(l_id);
            else
                dtModule = BO_Module.SelectLocation(l_id);
            base.bindRepeater(rptModule, dtModule, divMessage);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void rptModule_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            base.OpenConnection();
            int c_id;
            int l_id;
            if (Request["l_id"] != null && Request["c_id"] != null)
            {
                c_id = Convert.ToInt32(Request["c_id"]);
                l_id = Convert.ToInt32(Request["l_id"]);
            }
            else
            {
                divMessage.InnerHtml = O2S_Message.Warning("Thông Báo", "Bản ghi này không tồn tại hoặc đã bị xóa .");
                return;
            }
            HiddenField hdfID = (HiddenField)e.Item.FindControl("hdfID");
            int m_id = Convert.ToInt32(hdfID.Value);
            string commandName = e.CommandName;
            if (commandName.Equals("del"))
            {
                BO_Module.Delete(m_id);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
            }
            else if (commandName.Equals("DisHome"))
                BO_Module.is_home(m_id);
            else if (commandName.Equals("DisDetail"))
                BO_Module.is_detail(m_id);
            loadModule(c_id, l_id);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void rptModule_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            base.OpenConnection();
            HiddenField hdfID = (HiddenField)e.Item.FindControl("hdfID");
            int m_id = Convert.ToInt32(hdfID.Value);
            DataTable objDT = BO_Module.Search(m_id);
            int type_id;
            if (objDT != null && objDT.Rows.Count > 0)
            {
                type_id = Convert.ToInt32(objDT.Rows[0]["TYPE_ID"]);
                HtmlAnchor aGoDetails = (HtmlAnchor)e.Item.FindControl("aGoDetails");
                switch (type_id)
                {
                    case 2:
                    case 3:
                    case 7:
                    case 13:
                    case 19:
                    case 24:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=shows&m_id=" + m_id;
                        break;
                    case 4:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=advertising&m_id=" + m_id;
                        break;
                    case 9:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=dealoftheday&m_id=" + m_id;
                        break;
                    case 10:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=support&m_id=" + m_id;
                        break;
                    case 12:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=vote&m_id=" + m_id;
                        break;
                    case 14:
                    case 5:
                    case 21:
                    case 22:
                    case 23:
                    case 27:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=news&m_id=" + m_id;
                        break;
                    case 15:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=video&m_id=" + m_id;
                        break;
                    case 16:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=news&m_id=" + m_id;
                        break;
                    case 18:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=dealoftheday&m_id=" + m_id;
                        break;
                    case 6:
                        aGoDetails.HRef = "~/administrator/default.aspx?module=products&m_id=" + m_id;
                        break;
                    default:
                        aGoDetails.HRef = "";
                        aGoDetails.Style.Add("Color", "Black");
                        break;
                }
            }
            Button btnDisDetail = (Button)e.Item.FindControl("btnDisDetail");
            Button btnDisHome = (Button)e.Item.FindControl("btnDisHome");
            bool isdetail = Convert.ToBoolean(btnDisDetail.CommandArgument);
            if (!isdetail)
                btnDisDetail.CssClass = "icon-32-cancel";
            else
                btnDisDetail.CssClass = "icon-32-checking";
            bool ishome = Convert.ToBoolean(btnDisHome.CommandArgument);
            if (!ishome)
                btnDisHome.CssClass = "icon-32-cancel";
            else
                btnDisHome.CssClass = "icon-32-checking";
        }
        catch { }
        finally { base.CloseConnection(); }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        if (Request["c_id"] != null)
            Response.Redirect("default.aspx?module=moduleadd&c_id=" + Request["c_id"].ToString());
        else
            Response.Redirect("default.aspx?module=moduleadd");
    }
    private void loadCategories()
    {
        ddlCategories.Items.Clear();
        ddlCategories.Items.Add(new ListItem("Modules trang chủ", "-2"));
        ddlCategories.Items.Add(new ListItem("Tất cả các Modules", "-1"));
        ddlCategories.Items.Add(new ListItem("Đăng ký", "-3"));
        ddlCategories.Items.Add(new ListItem("Quên mật khẩu", "-4"));
        ddlCategories.Items.Add(new ListItem("Shows Chi tiết", "-5"));
        ddlCategories.Items.Add(new ListItem("Tin chi tiết", "-6"));
        ddlCategories.Items.Add(new ListItem("Liên hệ", "-7"));
        base.loadCategories(ddlCategories, 0);
    }
    protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = base.removeRequest();
        if (Request["c_id"] != null)
        {
            int conti = url.LastIndexOf("c_id=");
            string start = url.Substring(0, url.LastIndexOf("&c_id="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        Response.Redirect(url + "&c_id=" + ddlCategories.SelectedValue);
    }
}
